'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async add() {
    const {ctx,app,service} = this;
    var name = ctx.request.body.name || "";
    if(!name){
        ctx.error(app.resCode.params);
    }else{
        try{
            let param = ctx.request.body;
            param.uid = ctx.session.userinfo._id;
            let res = await service.person.add(param);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        }
    }
  }
  async webAdd(){
    const {ctx,app,service} = this;
    var body = ctx.request.body || "";
    if(!body.name || !body.uid){
        ctx.error(app.resCode.params);
    }else{
        
        try{
            let res = await service.person.add(body);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        }
    }
  }
  async remove(){
    const {ctx,app,service} = this;
    let id = ctx.request.body.id || "";
    if(!id){
     ctx.error(app.resCode.params);
    }else{
         try{
             let res = await service.person.remove(id);
             ctx.success(res);
         }catch(e){
             ctx.error(app.resCode.params,e.message || e);
         }
    }
    }
    async update(){
        const {ctx,app,service} = this;
        let param = ctx.request.body;
        if(!param.id && !param._id){
            ctx.error(app.resCode.params);
        }else{
            try{
                let res = await service.person.update(param);
                ctx.success(res);
            }catch(e){
                ctx.error(app.resCode.params,e.message || e);
            }
        }
   }
   async list(){
    const {ctx,app,service} = this;
    let query = ctx.query;
    try{
        query.uid = ctx.session.userinfo._id;
        let res = await service.person.list(query);
        ctx.success(res);
    }catch(e){
        ctx.error(app.resCode.params,e.message || e);
    }
   }
   async webList(){
    const {ctx,app,service} = this;
    let query = ctx.query;
    if(!query.uid){
        ctx.error(app.resCode.params);
    }else{
        try{
            let res = await service.person.list(query);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        }
    }
   }
   async info(){
    const {ctx,app,service} = this;
    let id = ctx.query.id;
    if(!id){
        ctx.error(app.resCode.params);
    }else{
        try{
            let res = await service.person.info(id);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        } 
    }
   }
   async updateOrders(){
    const {ctx,app,service} = this;
    let param = ctx.request.body;
    if(!param.list){
        ctx.error(app.resCode.params);
    }else{
        try{
            param.uid = ctx.session.userinfo._id;
            let res = await service.person.updateOrders(param);
            ctx.success(res);
        }catch(e){
            ctx.error(app.resCode.params,e.message || e);
        } 
    }
   }
}

module.exports = HomeController;
